package org.holdren.testspringmvc.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.holdren.testspringmvc.ejb.PersonService;
import org.holdren.testspringmvc.model.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;

@Controller
public class HomeController 
{
	//@EJB(mappedName="java:module/PersonService")
	@Autowired
	private PersonService personService;
	
	@RequestMapping(value="/")
	public ModelAndView test(HttpServletResponse response) throws IOException
	{
		return new ModelAndView("home");
	}
	
	@RequestMapping(value="/brian")
	public String brian()
	{
		return "brian";
	}
	
	@RequestMapping(value="/person/{personId}", method=RequestMethod.GET)
	public String getPerson(@PathVariable int personId, ModelMap model) throws Exception
	{
		System.out.println("getting person id:"+personId);
		Person p = personService.getPerson(personId);
		
		model.addAttribute("model", p);
		
		return "person";
	}
	
	@RequestMapping(value="/person/", method=RequestMethod.POST)
	public ModelAndView createPerson(@RequestBody Person person, HttpServletResponse httpResponse, WebRequest request)
	{
		System.out.println("creating person:"+person);
		person.setId(685913);
		return new ModelAndView(new MappingJacksonJsonView(), "model", person);
	}
}
